//
//  MoviePlayer.m
//  CMS
//
//  Created by Vuong Van Khanh on 12/11/12.
//  Copyright (c) 2012 Rikkei. All rights reserved.
//

#import "MoviePlayer.h"
#import <MediaPlayer/MediaPlayer.h>
#import "MoviePlayerUserPrefs.h"

CGFloat movieViewOffsetX = 30.0;
CGFloat movieViewOffsetY = 30.0;
@interface MoviePlayer (OverlayView)

- (void)adjustViewsForOrientation:(UIInterfaceOrientation)orientation;

@end

@implementation MoviePlayer

@synthesize backgroundView;
@synthesize url;
@synthesize moviePlayerController;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


#pragma mark - View lifecycle

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES];
    if(url)
    { 
//        moviePlayerController = [[MPMoviePlayerViewController alloc] 
//         initWithContentURL:url];
        moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:url];
        [moviePlayerController setContentURL:url];
        //set default action when movie finished
        [[NSNotificationCenter defaultCenter] 
         addObserver:self
         selector:@selector(movieFinishedCallback:)
         name:MPMoviePlayerPlaybackDidFinishNotification
         object:moviePlayerController];
        //set action when press done button
//        [[NSNotificationCenter defaultCenter] addObserver:self 
//                                                 selector:@selector(doneButtonClick:) 
//                                                     name:MPMoviePlayerWillExitFullscreenNotification 
//                                                   object:nil];
        
        CGRect viewInsetRect = CGRectInset ([self.view bounds],
                                            movieViewOffsetX,
                                            movieViewOffsetY );

        moviePlayerController.view.frame = viewInsetRect;
        [self.view addSubview:moviePlayerController.view];
        [moviePlayerController setMovieSourceType:MPMovieSourceTypeFile];
        
        //---play movie---
        [moviePlayerController play];
        
    }
    else
        [self.navigationController popViewControllerAnimated:YES];
}
-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
    MPMoviePlayerController *player = [aNotification object];
    [[NSNotificationCenter defaultCenter] 
     removeObserver:self
     name:MPMoviePlayerPlaybackDidFinishNotification
     object:player];
    [player stop];
    [self.navigationController setNavigationBarHidden:NO];
}

//- (void) doneButtonClick:(NSNotification*) aNotification{
//    MPMoviePlayerController *player = [aNotification object];
//    [[NSNotificationCenter defaultCenter] removeObserver:self 
//                                          name:MPMoviePlayerWillExitFullscreenNotification 
//                                        object:player];
////    [player.view removeFromSuperview];
//    [player stop];
//    [self.navigationController popViewControllerAnimated:YES];
//}
- (void)viewDidUnload
{
//    [self deletePlayerAndNotificationObservers];
    [self setBackgroundView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

#pragma mark View Controller

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    [self adjustViewsForOrientation:interfaceOrientation];
    // Return YES for supported orientations
	return YES;
}

- (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation {
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
        moviePlayerController.view.center = CGPointMake(512.0f, 384.0f);
        CGRect viewInsetRect = CGRectInset ([self.view bounds],
                                            movieViewOffsetX,
                                            movieViewOffsetY );
        
        moviePlayerController.view.frame = viewInsetRect;

    }
    else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
        moviePlayerController.view.center = CGPointMake(384.0f, 512.0f);
        CGRect viewInsetRect = CGRectInset ([self.view bounds],
                                            movieViewOffsetX,
                                            movieViewOffsetY );
        
        moviePlayerController.view.frame = viewInsetRect;

    }
}

- (void)dealloc 
{	
    [self setMoviePlayerController:nil];
    self.backgroundView = nil;
}
@end
